Thread: How to read file to array...help please

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    Question How to read file to array...help please

    Write a program that

    a. Opens the data file noisedata.txt and reads the dc, gt, and snr values into arrays. (You must use the
    exact file provided with the problem statement, including the text headings.)

    Noisedata.txt
    This data file is arranged as follows
    The number on a lone line is the dopant concentration in atoms/cucm x 1.00E-17
    The next five lines are the gt in nanometers
    followed by the signal to noise ratio in decibels
    1
    2 35.1
    2.2 36.3
    2.4 37.2
    2.6 38.1
    2.8 39.5

    1.2
    2 37.2
    2.2 39.5
    2.4 41.2
    2.6 42.3
    2.8 46.2

    1.4
    2 41.2
    2.2 44.2
    2.4 48.2
    2.6 50.3
    2.8 53.4

    1.6
    2 48.2
    2.2 51.2
    2.4 55.6
    2.6 60.2
    2.8 65.2

    1.8
    2 55.6
    2.2 62.3
    2.4 66.2
    2.6 72.5
    2.8 78.6

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main (void) 
    {
    	printf("\nLinear Regression Results of snr vs. gt: snr = gt*slope + intercept\n\n");
    	printf("dc <atoms/cc*10e-17>   slope     intercept      r-squared\n");
    	printf("-----------------------------------------------------------\n");
    	
    	
    	FILE *inFilePtr;
    	inFilePtr = fopen("noisedata.rtf","r");
    	
    	char dc[5];
    	char gt[25];
    	char snr[25];
    	
    	if ( inFilePtr == NULL )
    		
    		
    		printf("\nFile cannot be opened\n");
    	
    	else
    		
    		printf("\nContents of noisedata.rtf\n\n");
    		fscanf(inFilePtr, "%s", dc);
        
    	while ( !feof(inFilePtr) ) 
    	{
    		
    		printf("%s\n", dc);
    		fscanf(inFilePtr, "%s", dc);
    	}
    	return 0;
    }

    do not know if i'm going in the right direction or not.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The file is strictly formatted, but I'd still use fgets() rather than the "brittle", fscanf(). Once you have the data safely into a char array, then you can use sscanf() to "pick apart" at it, with careful logic.

    6 lines of numbers, and all but the first row has 2 numbers (floats generally), each.

    There's the first thing I'd be concerned with. Sometimes that first number in the row, is an integer, and other times, it's a float. Picking that out from the string will be your first challenge.

    I'd do it the same way you'd do it with pen and paper - working carefully from left to right, as you get the data into the array from the strings.

    Don't code yet - you're not ready. Figure out the algorithm to do this, and *then* put it into pseudo code, followed by compilable code.

    Worst thing you can do, is to sit down and start writing up code for a problem you don't know how to solve yet, yourself. It can work later on, (when you don't have the algorithm, but you have a good idea for one), but when you're a beginner, it's totally frustrating and a huge waste of time and effort.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Read from file to an array
    By brooklyn1126 in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2004, 08:32 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

Tags for this Thread